const express = require("express");
const app = express();

const jwt = require("jsonwebtoken");
const expressjwt = require("express-jwt");

const cors = require("cors");
app.use(cors());

app.use(express.json());
app.use(express.urlencoded({ extended: false }));

/**
 *
 * sign（）
 * 参数1：用户的信息对象
 * 参数2：加密的密钥
 * 参数3：配置对象 ，可以配置 当前 token 的有效期
 * 切不可将密码加上
 */
const secretKey = "~hEmkz_n8yXaasbv";
app.post("/api/login", (req, res) => {
  //   res.setHeader("Access-Control-Allow-Origin", "*");
  res.send({
    status: 200,
    message: "login successfully",
    token: jwt.sign({ username: "fan" }, secretKey, { expiresIn: "60s" }),
  });
});

app.listen(80, () => {
  console.log("Express Server running at http://127.0.0.1");
});
